<?php

/**
 * sfFormGuiQuestion actions.
 *
 * @package    plugintest
 * @subpackage sfFormGuiQuestion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class sfFormGuiQuestionActions extends sfActions
{
  public function executeIndex($request)
  {
    $this->sf_form_gui = sfFormGuiPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->sf_form_gui);
    $this->c = new Criteria;
    $this->c->addAscendingOrderByColumn(sfFormGuiQuestionPeer::RANK);
  }

  public function executeCreate()
  {
    $this->form = new sfFormGuiQuestionForm();

    $this->setTemplate('edit');
  }

  public function executeEdit($request)
  {
    $this->form = new sfFormGuiQuestionForm(sfFormGuiQuestionPeer::retrieveByPk($request->getParameter('id')));
  }

  public function executeUpdate($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new sfFormGuiQuestionForm(sfFormGuiQuestionPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('sf_form_gui_question'));
    if ($this->form->isValid())
    {
      $sf_form_gui_question = $this->form->save();

      $this->redirect('sfFormGui/index');
    }

    $this->setTemplate('edit');
  }

  public function executeDelete($request)
  {
    $this->forward404Unless($sf_form_gui_question = sfFormGuiQuestionPeer::retrieveByPk($request->getParameter('id')));

    $sf_form_gui_question->delete();

    $this->redirect('sfFormGuiQuestion/index');
  }
  
  public function executeSort($request)
  {
      $order = $request->getParameter('order-'.$this->getModuleName().'-'.$request->getParameter('id'));
      $this->forward404Unless(is_array($order));
      $flag = sfFormGuiQuestionPeer::doSort($order);
      return $this->renderText($this->getContext()->getI18N()->__($flag ? 'Status updated' : 'Error' ));
  }
  
    public function executeStats($request) {
        $this->sf_form_gui_question = sfFormGuiQuestionPeer::retrieveByPk($request->getParameter('id'));
        $this->forward404Unless($this->sf_form_gui_question);
        
        $graph = new sfJpGraph('pie3D', $request->getParameter('width', 500), $request->getParameter('height', 200));
        $graph = $graph->getJpGraph();
        $graph->SetShadow();
        $graph->SetMargin(40,20,60,20);
        $graph->SetColor('black'); 
        $graph->title->Set($this->getContext()->getI18N()->__('Répartition des réponses'));
        $graph->title->SetFont(FF_ARIAL,FS_BOLD);
        $graph->SetFrame(false);
        $graph->SetBox(true);
        
        $stats = $this->sf_form_gui_question->getStats();
        $this->forward404Unless($stats);
        
        $pie = new PiePlot3D($stats['values']);
        $pie->ExplodeAll(5);
        $pie->setLegends($stats['keys']);
        $graph->Add($pie);
            
        $graph->Stroke();
        return sfView::NONE;
    }

}
